package com.liaowei.user.controller;

import com.liaowei.common.BaseResponse;
import com.liaowei.common.ResultUtils;
import com.liaowei.model.domain.Post;
import com.liaowei.model.param.PostHistoryParam;
import com.liaowei.user.model.param.PagePostHistoryParam;
import com.liaowei.user.service.UserPostHistoryService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Min;
import java.util.List;

@RestController
@RequestMapping("posts/histories")
@RequiredArgsConstructor
public class UserPostHistoryController {

    private final UserPostHistoryService userPostHistoryService;

    // 分页查询自己的帖子历史记录
    @PostMapping("page")
    public BaseResponse<List<Post>> postHistory(@RequestBody
                                                @Validated
                                                PagePostHistoryParam historyParam) {
        return ResultUtils.success(userPostHistoryService.searchPostHistoryPage(historyParam));
    }

    // 根据用户id和帖子id删除帖子记录
    @DeleteMapping("{userId}/{postId}")
    public BaseResponse<String> deleteArticleHistoryByUserIdAndPostId(@PathVariable
                                                                      @Min(value = 1, message = "请输入合法id")
                                                                      Long userId,
                                                                      @PathVariable
                                                                      @Min(value = 1, message = "请输入合法id")
                                                                      Long postId) {
        return ResultUtils.success(userPostHistoryService.
                removePostHistoryByUserIdAndPostId(userId, postId));
    }


    // 新增帖子历史记录
    @PostMapping
    public BaseResponse<String> addPostHistory(@RequestBody @Validated PostHistoryParam postHistoryParam) {
        return ResultUtils.success(userPostHistoryService.addPostHistory(postHistoryParam));
    }
}
